<?php

namespace App\AbstractFactory\ArmyUnit;

use App\AbstractFactory\UnitAbstract;
use App\AbstractFactory\UnitInterface;
use App\AbstractFactory\CompositeUnitAbstract;

class Army extends CompositeUnitAbstract {

    private $units = [];

    public function addUnit(UnitInterface $unit)
    {
        if (in_array($unit, $this->units, true)) {
            return true;
        }
        $this->units[] = $unit;
    }

    public function removeUnit(UnitInterface $unit)
    {
        $this->units = array_udiff($this->units, [$unit], function ($a, $b) {
            return $a === $b ? false : 1;
        });
    }

    public function bombardStrength()
    {
        $ret = 0;
        foreach ($this->units as $unit) {
            $ret += $unit->bombardStrength();
        }
        return $ret;
    }
}
